package com.ruoyi.bussiness.service.impl;

import java.util.List;

import com.ruoyi.bussiness.domain.SysBusinessExpert;
import com.ruoyi.bussiness.domain.SysBusinessExpert;
import com.ruoyi.bussiness.mapper.SysBusinessExpertMapper;
import com.ruoyi.bussiness.service.ISysBusinessExpertService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 学者风采Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-23
 */
@Service
public class SysBusinessExpertServiceImpl implements ISysBusinessExpertService
{
    @Autowired
    private SysBusinessExpertMapper sysBusinessExpertMapper;

    /**
     * 查询学者风采
     *
     * @param id 学者风采主键
     * @return 学者风采
     */
    @Override
    public SysBusinessExpert selectSysBusinessExpertById(Long id)
    {
        return sysBusinessExpertMapper.selectSysBusinessExpertById(id);
    }

    /**
     * 查询学者风采列表
     *
     * @param sysBusinessExpert 学者风采
     * @return 学者风采
     */
    @Override
    public List<SysBusinessExpert> selectSysBusinessExpertList(SysBusinessExpert sysBusinessExpert)
    {
        return sysBusinessExpertMapper.selectSysBusinessExpertList(sysBusinessExpert);
    }

    /**
     * 新增学者风采
     *
     * @param sysBusinessExpert 学者风采
     * @return 结果
     */
    @Override
    public int insertSysBusinessExpert(SysBusinessExpert sysBusinessExpert)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        sysBusinessExpert.setCreateBy(sysUser.getUserName());
        sysBusinessExpert.setCreateTime(DateUtils.getNowDate());
        sysBusinessExpert.setIsBottom("0");
        sysBusinessExpert.setIsTop("0");

        sysBusinessExpertMapper.insertSysBusinessExpert(sysBusinessExpert);

        sysBusinessExpertMapper.updateOrderNum(sysBusinessExpert.getId());
        return 1;
//        return sysBusinessExpertMapper.insertSysBusinessExpert(sysBusinessExpert);
    }

    /**
     * 修改学者风采
     *
     * @param sysBusinessExpert 学者风采
     * @return 结果
     */
    @Override
    public int updateSysBusinessExpert(SysBusinessExpert sysBusinessExpert)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        sysBusinessExpert.setUpdateBy(sysUser.getUserName());
        sysBusinessExpert.setUpdateTime(DateUtils.getNowDate());
        return sysBusinessExpertMapper.updateSysBusinessExpert(sysBusinessExpert);
    }

    /**
     * 批量删除学者风采
     *
     * @param ids 需要删除的学者风采主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessExpertByIds(Long[] ids)
    {
        return sysBusinessExpertMapper.deleteSysBusinessExpertByIds(ids);
    }

    /**
     * 删除学者风采信息
     *
     * @param id 学者风采主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessExpertById(Long id)
    {
        return sysBusinessExpertMapper.deleteSysBusinessExpertById(id);
    }

    @Override
    public AjaxResult moveUpOrDown(SysBusinessExpert entity) {
        SysBusinessExpert moveEntity = sysBusinessExpertMapper.selectSysBusinessExpertById(entity.getId());
        if(moveEntity == null){
            return AjaxResult.error("动态类别数据不存在，不能上移下移");
        }
        SysBusinessExpert moveChangeEntity = null;
        if("up".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessExpertMapper.getLastList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("首行数据不能上移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessExpertMapper.updateSysBusinessExpert(moveEntity);
            sysBusinessExpertMapper.updateSysBusinessExpert(moveChangeEntity);
        }else if("down".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessExpertMapper.getNextList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("末行数据不能下移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessExpertMapper.updateSysBusinessExpert(moveEntity);
            sysBusinessExpertMapper.updateSysBusinessExpert(moveChangeEntity);
        }else{
            return AjaxResult.error("无效的移动方式");
        }
        return AjaxResult.success("移动成功",1);
    }

    @Override
    public AjaxResult pinToTopOrEnd(SysBusinessExpert entity) {
        SysBusinessExpert moveEntity = sysBusinessExpertMapper.selectSysBusinessExpertById(entity.getId());
        if(moveEntity == null){
            return AjaxResult.error("动态类别不存在，不能上移下移");
        }
        if("top".equalsIgnoreCase(entity.getPinType())){
            if("1".equalsIgnoreCase(moveEntity.getIsTop())){
                moveEntity.setIsTop("0");
                moveEntity.setIsBottom("0");
            }else{
                moveEntity.setIsTop("1");
                moveEntity.setIsBottom("0");
            }
            sysBusinessExpertMapper.updateSysBusinessExpert(moveEntity);
        }else if("end".equalsIgnoreCase(entity.getPinType())){
            if("1".equalsIgnoreCase(moveEntity.getIsBottom())){
                moveEntity.setIsBottom("0");
                moveEntity.setIsTop("0");
            }else{
                moveEntity.setIsBottom("1");
                moveEntity.setIsTop("0");
            }
            sysBusinessExpertMapper.updateSysBusinessExpert(moveEntity);
        }else{
            return AjaxResult.error("无效的操作方式");
        }
        return AjaxResult.success("置顶置底成功",1);
    }
}
